package org.gwu.seuss.communication.util;

import java.io.File;
import java.io.IOException;

import org.apache.commons.lang3.StringUtils;

public class FileUtils {

	/**
	 * 
	 * @param filename
	 * @param location
	 * @param fileArray
	 * @throws Exception
	 */
	public static void writeFile(String filename, String location, byte[] fileArray) throws Exception {

		if (StringUtils.isBlank(location)) {
			location = "";
		} else {
			location = location + File.separatorChar;
		}

		org.apache.commons.io.FileUtils.writeByteArrayToFile(new File(location + filename), fileArray);
	}

	/**
	 * 
	 * @param filename
	 * @param location
	 * @return
	 * @throws Exception
	 */
	public static byte[] readFile(String filename, String location) throws Exception {
		if (StringUtils.isBlank(location)) {
			location = "";
		} else {
			location = location + File.separatorChar;
		}

		File file = new File(location + filename);
		return org.apache.commons.io.FileUtils.readFileToByteArray(file);
	}
	
	public static void deleteFile(String filename, String location) throws IOException {
		if (StringUtils.isBlank(location)) {
			location = "";
		} else {
			location = location + File.separatorChar;
		}
		
		File file = new File(location + filename);
		org.apache.commons.io.FileUtils.forceDelete(file);
	}
	
	/**
	 * 
	 * @param directory
	 * @throws IOException
	 */
	public static void createDirectory(String directory) throws IOException {
		File dir = new File(directory);
		
		if(!dir.exists()) {
			org.apache.commons.io.FileUtils.forceMkdir(dir);
		} else {
			// Do Nothing
		}
	}
}
